﻿using Mozhi.Abp.Core.Extensions;
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp;

namespace mozhi.smarterp.Finance.Finances
{
    public class Settlement : AggregateRootBase<Guid>
    {
        public int Period { private set; get; }

        public bool Closed { private set; get; }

        public Settlement(Guid id, int period)
        {
            this.Period = period;
            this.Id = id;

            Validate();
        }

        public void Close(int initalizePeriod, bool prePeriodClosed)
        {
            if (Closed)
                return;

            if (initalizePeriod == Period)
            {
                //表示完成初始化，可以直接关闭
                Closed = true;
                return;
            }

            if (!prePeriodClosed)
                throw new UserFriendlyException($"上一期还未结账，当前期无法结账", "100000");

            Closed = true;

        }

        public void Open(int initalizePeriod, bool nextPeriodClosed)
        {
            var tpis = initalizePeriod == Period ? $"存在结账期间，无法反初始化" : $"期间：{Period.ToNextPeriod()}已结账，无法反结账";

            if (nextPeriodClosed)
                throw new UserFriendlyException(tpis, "100000");

            Closed = false;
        }

        private void Validate()
        {
            if (Period == 0)
                throw new UserFriendlyException("结账期间不能为空", "100000");
        }
    }
}
